<template>
  <div id="tradeRecord">
    <div class="fixed">
      <NavBar title="实时数据" left-arrow @click-left="onClickLeft" />
      <tabs class="tabBox" v-model="active">
        <tab
          :title="item"
          :name="index"
          v-for="(item, index) in tabItem"
          :key="item"
          :text="item"
        />
      </tabs>
    </div>
    <div class="dataBox" v-show="active == 0">
      <div class="padding">
        <div class="data">
          <div class="flex"><span>价格(USDT)</span> <span>数量(ETH)</span></div>
          <div class="flex" v-for="(item, index) in datas" :key="index">
            <template v-if="index < 10 && item">
              <span class="green">{{ item.price }}</span>
              <span>{{ item.amount.toFixed(6) }}</span>
            </template>
          </div>
        </div>
        <div class="data">
          <div class="flex"><span>价格(USDT)</span> <span>数量(ETH)</span></div>
          <div v-for="(item, index) in datas" :key="index">
            <div class="flex" v-if="index > 9 && item">
              <span class="red">{{ item.price }}</span>
              <span>{{ item.amount.toFixed(6) }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="dataBox clinchDeal" v-show="active == 1">
      <div class="top">
        <div>产品</div>
        <div>成交价</div>
        <div>方向</div>
        <div>时间</div>
      </div>
      <div
        class="clinchDealData top"
        v-for="(item, index) in symbolsArray"
        :key="index"
      >
        <template v-if="ch[`market.${item.symbol}.trade.detail`]">
          <div>{{ item.title }}</div>
          <div>
            {{ ch[`market.${item.symbol}.trade.detail`].tick.data[0].price }}
          </div>
          <div
            :class="
              ch[`market.${item.symbol}.trade.detail`].tick.data[0].direction ==
              'buy'
                ? 'green'
                : 'red'
            "
          >
            {{
              ch[`market.${item.symbol}.trade.detail`].tick.data[0].direction ==
              'buy'
                ? '买入'
                : '卖出'
            }}
          </div>
          <div>
            {{ ch[`market.${item.symbol}.trade.detail`].tick.data[0].date }}
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import { Tab, Tabs, NavBar } from 'vant'

import { EventBus } from '@/utils/event-bus'

export default {
  components: {
    Tab,
    Tabs,
    NavBar
  },
  data() {
    return {
      tabItem: ['实时数据', '实时成交'],
      // 币种列表
      symbolsArray: [
        { symbol: 'btcusdt', title: 'BTC/USDT' },
        { symbol: 'ethusdt', title: 'ETH/USDT' },
        { symbol: 'eosusdt', title: 'EOS/USDT' },
        { symbol: 'crousdt', title: 'CRO/USDT' },
        { symbol: 'trxusdt', title: 'TRX/USDT' }
      ],
      datas: [], //  实时数据
      ch: {}, //  实时成交
      active: 0 //  tab数值
    }
  },
  created() {
    this.active = Number(this.$route.query.type)
    // console.log(this.active, '------------------------------------------')
    let Interval = setInterval(() => {
			if (this.$store.state.websocket.is_open_socket) {
				this.send()
        this.clinchDealSend()
				// 已连接移除定时器
				clearInterval(Interval);
			}
		}, 500);
  },
  methods: {
    //  实时数据订阅
    send() {
      let ch = `market.${JSON.parse(localStorage.getItem('symbol')).symbol}.trade.detail`
      EventBus.$on(ch, res => {
        let newRes = res.data.tick.data.concat(this.datas)
        this.datas = newRes
        this.datas.length = 20
      })
    },
    //  实时成交订阅
    clinchDealSend() {
      this.symbolsArray.forEach(item => {
        let ch = `market.${item.symbol}.trade.detail`
        EventBus.$on(ch, res => {
          // 时间转换
          let date = new Date(res.data.tick.data[0].ts)
          res.data.tick.data[0].date = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`

          this.$set(this.ch, res.data.ch, res.data)
        })
      })
    },
    // 解决直接后退页面symbol被转译问题
    onClickLeft() {
      this.$router.push({
        path: '/market',
        query: { symbol: this.$route.query.symbol }
      })
    }
  }
}
</script>

<style lang="less" scoped>
#tradeRecord {
  background-color: #f7f6fb;
  min-height: 100vh;
  overflow: hidden;
  .fixed {
    width: 100%;
    position: fixed;
    top: 0;
    .tabBox {
      /deep/.van-tabs__wrap {
        margin: 10px 0;
      }
      /deep/.van-tab--active {
        font-size: 15px;
        color: rgb(41, 121, 255);
        font-weight: bold;
      }
      /deep/.van-tabs__line {
        width: 30px;
        background-color: rgb(41, 121, 255);
        height: 3px;
        border-radius: 3px;
      }
      /deep/.van-tabs__wrap {
        margin: 0;
      }
    }
  }
  .dataBox {
    margin: 0 auto;
    margin-top: 100px;
    width: 95%;
    background: #fff;
    border-radius: 8px;
    .data {
      width: 47%;
      .flex {
        padding: 5px 0;
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }
  }
  .clinchDeal {
    overflow: hidden;
    .top {
      margin: 10px 10px 0 10px;
      padding-bottom: 10px;
      display: flex;
      border-bottom: solid 1px #e6eaed;
      div:nth-child(1) {
        width: 111px;
        text-align: left;
        padding-left: 20px;
      }
      div:nth-child(2) {
        text-align: right;
        min-width: 80px;
      }
      div:nth-child(3) {
        width: 20%;
        text-align: center;
      }
      div:nth-child(4) {
        width: 20%;
        text-align: center;
      }
    }
    .clinchDealData {
      border-bottom: none;
      div:nth-child(1) {
        padding-left: 0;
      }

      div:nth-child(2) {
        text-align: right;
        min-width: 100px;
      }
    }
  }
  .padding {
    padding: 10px;
    display: flex;
    justify-content: space-between;
  }
}
</style>
